
<<<<<<< HEAD
//封装学生列表，增删改查调用
getStudentList()
function getStudentList() {

  axios.get('/students', {
    headers: {
      Authorization: localStorage.getItem('token')
    }
  }).then(({ data }) => {
    // console.log(data.data);

    //学员人数
    $(".total").html(data.data.length)
    //渲染学生列表
    $(".list").html(data.data.map(ele => {
      ele.gender = ele.gender === 0 ? '男' : '女'
      return `<tr>
                  <td>${ele.name}</td>
                  <td>${ele.age}</td>
                  <td>${ele.gender}</td>
                  <td>第${ele.group}组</td>
                  <td>${ele.hope_salary}</td>
                  <td>${ele.salary}</td>
                  <td>${ele.city}${ele.area}</td>
                  <td>
                    <a href="javascript:;" class="text-success mr-3"><i class="bi bi-pen" data-id=${ele.id}></i></a>
                    <a href="javascript:;" class="text-danger"><i class="bi bi-trash" data-id=${ele.id}></i></a>
                  </td>
                  </tr>`
    }).join(''))

  })

}



//模态框
$("#openModal").on('click', function (e) {

  $('#modal').modal('show')
  $(".modal-title").text('添加学员')
})








//渲染省份

axios.get('/api/province').then(res => {
  // console.log(res);

  let province = res.data.data.map(ele => {
    //ele值是省份
    return `<option value="${ele}">--${ele}--</option>`

  }).join('')
  $("#province").html(`<option value="">--省份--</option>${province}`)
})
//$("#province")   $("#city")   $("#area")


//省市区联动
$("#province").on('change', function () {

  // console.log($("#province").val());
  let province = $("#province").val()
  let city = $("#city").val()


  axios.get('/api/city', {
    params: {
      pname: province,
    }
  }).then(res => {
    let cityval = res.data.data.map(ele => {

      return `<option value="${ele}">--${ele}--</option>`
    }).join('')

    $("#city").html(`<option value="">--城市--</option>${cityval}`)

  })

  $("#city").on('change', function () {

    let city = $("#city").val()

    axios.get('/api/area', {
      params: {
        pname: province,
        cname: city
      }
    }).then(res => {
      // console.log(res.data.data);
      let areaval = res.data.data.map(ele => {
        return `<option value="${ele}">--${ele}--</option>`
      }).join('')
      $("#area").html(`<option value="">--地区--</option>${areaval}`)
    })

  })

})


//删除操作
$(".list").on('click', '.bi-trash', function (e) {
  e.preventDefault()
  let id = this.dataset.id
  console.log(id);

  axios.delete(`/students/${id}`).then(res => {
    getStudentList()
  })

})



//修改操作
$(".list").on('click', '.bi-pen', function (e) {
  //newModal
  $('#modal').modal('show')
  let id = this.dataset.id

  $(".modal-title").text('修改学员')

  axios.get(`/students/${id}`).then(({ data: { data } }) => {
    // console.log(res);

    axios.put(`/students/${id}`, data).then(({ data: { data } }) => {
      console.log(data);




      $("[name=name]").val(data.name)
      // $("[name=gender]").val()
      if (data.gender === 0) {
        $("#cb01").prop('checked', true)
      } else {
        $("#cb02").prop('checked', true)
      }
      $("[name=age]").val(data.age)
      $("[name=group]").val(data.group)
      $("[name=hope_salary]").val(data.hope_salary)
      $("[name=salary]").val(data.salary)
      $("[name=province]").val(data.province)


//       axios.get('/api/city', {
//         params: {
//           pname: data.province,
//         }
//       }).then(res=>{
// console.log(data.city);
//         const city = $("[name=city]").val(data.city)
//       })


    })

  })

})



//添加操作
$("#submit").on('click', function () {

  const gender = +$("[name=gender]:checked").val()
  const name = $("[name=name]").val()
  const age = +$("[name=age]").val()
  const hope_salary = +$("[name=hope_salary]").val()
  const group = +$("[name=group]").val()
  const salary = +$("[name=salary]").val()
  const province = $("[name=province]").val()
  const city = $("[name=city]").val()
  const area = $("[name=area]").val()






  console.log(gender);
  // console.log(id);



  axios.post('/students', { gender, name, age, hope_salary, group, salary, province, city, area }).then((res) => {
    console.log(res);


    getStudentList()
    $('#modal').modal('hide')
    $('#form')[0].reset()
  })


})

=======

  axios.get('/students').then((message, data) => {
    console.log(message, data);
  })


>>>>>>> student

